package com.ruoyi.common.utils;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.LineSeparator;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

public class PdfUtil {

    // 自定义页脚事件类
    private static class FooterPageEvent extends PdfPageEventHelper {
        private Font content;

        public FooterPageEvent(Font content) {
            this.content = content;
        }

        @Override
        public void onEndPage(PdfWriter writer, Document document) {
            try {
                PdfContentByte canvas = writer.getDirectContent();
                // 设置线条颜色
                canvas.setColorStroke(BaseColor.RED);

                // 绘制粗线 (距离底部50点)
                canvas.setLineWidth(3f);
                float yPosition = 50;
                canvas.moveTo(document.left(), yPosition);
                canvas.lineTo(document.right(), yPosition);
                canvas.stroke();

                // 绘制细线 (距离底部45点)
                canvas.setLineWidth(1f);
                yPosition = 45;
                canvas.moveTo(document.left(), yPosition);
                canvas.lineTo(document.right(), yPosition);
                canvas.stroke();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void setResponseContentType(HttpServletResponse response, String fileName) throws UnsupportedEncodingException {
        response.setContentType("application/pdf");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8") + ".pdf");
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
    }

    public static void downloadPdf(Map<String,String> dataMap, HttpServletResponse response){
        // 定义全局的字体静态变量
        Font titlefont;
        Font headfont;
        Font keyfont = null;
        Font textfont = null;
        Font content = null;
        Font space = null;
        Font space1 = null;
        Font space2 = null;
        Font space3 = null;
        // 最大宽度
        try {
            // 不同字体（这里定义为同一种字体：包含不同字号、不同style）
            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            titlefont = new Font(bfChinese, 16, Font.BOLD);
            headfont = new Font(bfChinese, 14, Font.BOLD);
            keyfont = new Font(bfChinese, 22, Font.BOLD);
            textfont = new Font(bfChinese, 15, Font.NORMAL);
            content = new Font(bfChinese, 16, Font.NORMAL);
            space = new Font(bfChinese, 5, Font.NORMAL);
            space1 = new Font(bfChinese, 20, Font.NORMAL);
            space2 = new Font(bfChinese, 20, Font.NORMAL);
            space3 = new Font(bfChinese, 3, Font.NORMAL);

        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        BaseFont bf;
        Font font = null;
        try {
            //创建字体
            bf = BaseFont.createFont( "STSong-Light", "UniGB-UCS2-H",
                BaseFont.NOT_EMBEDDED);
            //使用字体并给出颜色
            font = new Font(bf,36,Font.BOLD, BaseColor.RED);
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        Document document = new Document(new Rectangle(PageSize.A4));
        try {
            PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
            // 注册页脚事件，将页脚固定在页面底部
            writer.setPageEvent(new FooterPageEvent(content));
            //打开PDF文件
            document.open();

            //设置标题
            Paragraph paragraph = new Paragraph("梁平区聚力打造营商环境“金名片”工作专班", new Font(bf,36,Font.BOLD, BaseColor.RED));
            //居中设置
            paragraph.setAlignment(Element.ALIGN_CENTER);
            document.add(paragraph);

            //页眉横线
            document.add(new Paragraph("\n", space2));
            LineSeparator line = new LineSeparator(3f, 100, BaseColor.RED, Element.ALIGN_CENTER, 0f);
            document.add(line);
            document.add(new Paragraph("\n", space3));
            LineSeparator lineStart = new LineSeparator(1f, 100, BaseColor.RED, Element.ALIGN_CENTER, 0f);
            document.add(lineStart);

            //投诉编号
            document.add(new Paragraph("\n", space));
            String text = "〔2025〕- " + dataMap.get("id");
            Paragraph paragraph0 = new Paragraph(text, content);
            paragraph0.setAlignment(Element.ALIGN_RIGHT);
            document.add(paragraph0);

            //副标题
            document.add(new Paragraph("\n"));
            Paragraph paragraph1 = new Paragraph("营商环境问题投诉", new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 24, Font.NORMAL));
            paragraph1.setAlignment(Element.ALIGN_CENTER);
            document.add(paragraph1);

            //问题来源
            document.add(new Paragraph("\n"));
            StringBuilder paragraph2Content = new StringBuilder();
            if (dataMap.get("enterprise") != null && !dataMap.get("enterprise").equals("")) {
                paragraph2Content.append(dataMap.get("enterprise"));
            }
            if (dataMap.get("name") != null && !dataMap.get("name").equals("")) {
                paragraph2Content.append("、" + dataMap.get("name"));
            }
            if (dataMap.get("mobile") != null && !dataMap.get("mobile").equals("")) {
                paragraph2Content.append("、" + dataMap.get("mobile"));
            }
            if (dataMap.get("email") != null && !dataMap.get("email").equals("")) {
                paragraph2Content.append("、" + dataMap.get("email"));
            }
            String sourceStr = paragraph2Content.toString().replaceAll("^、|、$", "").trim();
            Paragraph paragraph2 = new Paragraph("问题来源：" + sourceStr, content);
            paragraph2.setAlignment(Element.ALIGN_LEFT);
            document.add(paragraph2);

            //问题类型
            Paragraph paragraph3 = new Paragraph("问题类型：" + dataMap.get("type"), content);
            paragraph3.setAlignment(Element.ALIGN_LEFT);
            document.add(paragraph3);

            //牵头部门
            Paragraph paragraph4 = new Paragraph("牵头部门：" + dataMap.get("leadDept"), content);
            paragraph4.setAlignment(Element.ALIGN_LEFT);
            document.add(paragraph4);

            //问题描述
            Paragraph paragraph5 = new Paragraph("问题描述：", content);
            paragraph5.setAlignment(Element.ALIGN_LEFT);
            document.add(paragraph5);

            //描述内容
            Paragraph paragraph6 = new Paragraph(dataMap.get("content"), content);
            //设置首行缩进
            paragraph6.setFirstLineIndent(20);
            document.add(paragraph6);

            // 添加黑色分割线
            LineSeparator separator = new LineSeparator();
            separator.setLineColor(BaseColor.BLACK);
            document.add(new Chunk(separator));

            //审签流程
            Paragraph paragraph7 = new Paragraph("审签流程：", content);
            paragraph7.setAlignment(Element.ALIGN_LEFT);
            paragraph7.setFirstLineIndent(20);
            document.add(paragraph7);
            // 在段落前添加间距
            document.add(new Paragraph("\n", space));

            if (dataMap.get("circulationType").equals("1")) {
                // 内部流转审签
                Paragraph paragraph8 = new Paragraph();
                paragraph8.setAlignment(Element.ALIGN_LEFT);
                paragraph8.setFirstLineIndent(20);
                // 添加加粗的标题部分
                Chunk boldChunk = new Chunk("内部流转审签：", new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 16, Font.BOLD));
                paragraph8.add(boldChunk);
                // 添加不加粗的内容
                Chunk normalChunk = new Chunk("该问题应由" + dataMap.get("auditDept") + "回复，现提请领导审签。", content);
                paragraph8.add(normalChunk);
                document.add(paragraph8);

                //分管领导审签
                Paragraph paragraph9 = new Paragraph("分管领导审签：", content);
                paragraph9.setAlignment(Element.ALIGN_LEFT);
                paragraph9.setFirstLineIndent(20);
                document.add(paragraph9);

                //主要领导审签
                document.add(new Paragraph("\n"));
                document.add(new Paragraph("\n"));
                Paragraph paragraph10 = new Paragraph("主要领导审签：", content);
                paragraph10.setAlignment(Element.ALIGN_LEFT);
                paragraph10.setFirstLineIndent(20);
                document.add(paragraph10);

                //办理时限
                document.add(new Paragraph("\n"));
                document.add(new Paragraph("\n"));
                Paragraph paragraph11 = new Paragraph("办理时限：" + dataMap.get("timeLimit") + "个工作日", content);
                paragraph11.setAlignment(Element.ALIGN_LEFT);
                paragraph11.setFirstLineIndent(20);
                document.add(paragraph11);
            } else {
                // 外部流转审签
                Paragraph paragraph8 = new Paragraph();
                paragraph8.setAlignment(Element.ALIGN_LEFT);
                paragraph8.setFirstLineIndent(20);
                // 添加加粗的标题部分
                Chunk boldChunk = new Chunk("外部流转审签：", new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 16, Font.BOLD));
                paragraph8.add(boldChunk);
                // 添加不加粗的内容
                Chunk normalChunk = new Chunk("该问题应由" + dataMap.get("auditDept") + "回复，现提请领导审签。", content);
                paragraph8.add(normalChunk);
                document.add(paragraph8);

                //分管领导审签
                Paragraph paragraph9 = new Paragraph("分管领导审签：", content);
                paragraph9.setAlignment(Element.ALIGN_LEFT);
                paragraph9.setFirstLineIndent(20);
                document.add(paragraph9);

                //办理时限
                document.add(new Paragraph("\n"));
                document.add(new Paragraph("\n"));
                Paragraph paragraph10 = new Paragraph("办理时限：" + dataMap.get("timeLimit") + "个工作日", content);
                paragraph10.setAlignment(Element.ALIGN_LEFT);
                paragraph10.setFirstLineIndent(20);
                document.add(paragraph10);

                //办理结果反馈路径
                Paragraph paragraph11 = new Paragraph("办理结果反馈路径：", content);
                paragraph11.setAlignment(Element.ALIGN_LEFT);
                paragraph11.setFirstLineIndent(20);
                document.add(paragraph11);

                //1
                Paragraph paragraph12 = new Paragraph("1.通过OA系统反馈至发展改革委邮箱。", content);
                paragraph12.setAlignment(Element.ALIGN_LEFT);
                paragraph12.setFirstLineIndent(20);
                document.add(paragraph12);

                //2
                Paragraph paragraph13 = new Paragraph("2.办理结果反馈至市场主体相关联系人。", content);
                paragraph13.setAlignment(Element.ALIGN_LEFT);
                paragraph13.setFirstLineIndent(20);
                document.add(paragraph13);
            }
            //关闭文档
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
